<extend name="public/base"/>
<block name="css"></block>
<block name="body">
    <body>
    <div class="layui-fluid">
        <div class="layui-row layui-col-space15">
            <div class="layui-col-md12">
                <div class="layui-card">
                    <div class="layui-card-header ">
                        <span class="layui-breadcrumb">{{$crumbs|raw}}</span>
                        <a class="layui-btn layui-btn-sm" style="line-height:1.6em;margin-top:3px;float:right"
                           href="javascript:location.replace(location.href);" title="刷新">
                            <i class="layui-icon" style="line-height:30px">ဂ</i>
                        </a>
                    </div>
                    <div class="layui-card-header">
                        <button class="layui-btn layui-btn-danger layui-btn-sm delAll"><i class="layui-icon"></i>批量删除
                        </button>
                        <a class="layui-btn layui-btn-sm" onclick="parent.{{:add_tab('添加管理员','add')}}"><i
                                class="layui-icon"></i>添加
                        </a>
                    </div>
                    <div class="layui-card-body ">
                        <table class="layui-hide" id="test" lay-filter="demo"></table>
                        <script type="text/html" id="barDemo">
                            <a class="layui-btn layui-btn-xs" lay-event="auth"><i
                                    class="layui-icon layui-icon-release"></i>授权角色</a>
                            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i
                                    class="layui-icon layui-icon-edit"></i>编辑</a>
                            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i
                                    class="layui-icon layui-icon-delete"></i>删除</a>
                        </script>
                    </div>
                </div>
            </div>
        </div>
    </div>
    </body>
</block>
<block name="js">
    <script>
        layui.use(['table', 'jquery'], function () {
            var table = layui.table
            $ = layui.jquery,
                form = layui.form;
            table.render({
                elem: '#test'
                , url: '/admin/user/tree'
                , cellMinWidth: 25
                , cols: [[
                    {type: 'checkbox'}
                    , {field: 'id', title: 'ID', width: 50, unresize: true, sort: true}
                    , {
                        title: '账号'
                        , templet: function (d) {
                            let html = '<span>' + d.username + '</span>';
                            if (d.user_group[0]) {
                                html = html + '<span class="layui-badge">超级管理员</span>';
                            }
                            return html;
                        }
                    }
                    , {field: 'name', title: '名字'}
                    , {field: 'mobile', title: '手机号'}
                    , {field: 'email', title: '邮箱'}
                    , {
                        title: '状态', align: 'center'
                        , templet: function (d) {
                            let sel = d.status == 1 ? 'checked' : '';
                            return '<input type="checkbox" name="status" lay-filter="status" value="' + d.id + '" lay-skin="switch" lay-text="启用|禁用"' + sel + '>';
                        }
                    }
                    , {title: '操作', align: 'center', toolbar: '#barDemo'}
                ]]
                , page: true
            });
            //监听工具条
            table.on('tool(demo)', function (obj) {
                var data = obj.data;
                switch (obj.event) {
                    case 'auth':
                        xadmin.open('授权用户', '/admin/user/authUser?id=' + data.id);
                        break;
                    case 'del':
                        layer.confirm('确定要删除该角色吗？', function (index) {
                            $.ajax({
                                url: '/admin/user/delete',
                                method: 'post',
                                data: {id: data.id},
                                dataType: 'JSON',
                                success: function (res) {
                                    if (res.code == 0) {
                                        obj.del();
                                        layer.msg('删除成功', {icon: 1});
                                    } else {
                                        layer.msg(res.msg, {icon: 5});
                                    }
                                },
                                error: function (data) {
                                    layer.msg('服务器繁忙', {icon: 5});
                                }
                            });
                            layer.close(index);
                        });
                        break;
                    case 'edit':
                        xadmin.open('编辑', '/admin/user/edit?id=' + data.id);
                        // layer.alert('编辑行：<br>'+ JSON.stringify(data))
                        break;
                    default:
                        break;
                }
            });
            //ajax禁用开启
            form.on('switch(status)', function (obj) {
                // layer.tips(this.value + ' ' + this.name + '：' + obj.elem.checked, obj.othis);
                let data = {};
                data[this.name] = obj.elem.checked ? 1 : 0;
                let id = this.value;
                let __this = this;
                $.ajax({
                    url: '/admin/user/ajax/id/' + id,
                    method: 'post',
                    data: data,
                    dataType: 'JSON',
                    success: function (res) {
                        if (res.code == 0) {
                            layer.msg('修改成功', {icon: 1});
                        } else {
                            __this.checked = true;
                            form.render();
                            layer.msg(res.msg, {icon: 5});
                        }
                    },
                    error: function (data) {
                        layer.msg('服务器繁忙', {icon: 5});
                    }
                });
            });

            $('.delAll').on('click', function () {
                var checkStatus = table.checkStatus('test')
                    , data = checkStatus.data
                    , ids = '';
                if (data.length > 0) {
                    for (var i = 0; i < data.length; i++) {
                        ids = ids + data[i].id + ',';
                    }
                    var ids = (ids.substring(ids.length - 1) == ',') ? ids.substring(0, ids.length - 1) : ids;
                    layer.confirm('确定要删除' + data.length + '个用户吗？', function (index) {
                        $.ajax({
                            url: '/admin/user/delete',
                            method: 'post',
                            data: {id: ids},
                            dataType: 'JSON',
                            success: function (res) {
                                if (res.code == 0) {
                                    table.reload('test');
                                    layer.msg('删除成功', {icon: 1});
                                } else {
                                    layer.msg(res.msg, {icon: 5});
                                }
                            },
                            error: function (data) {
                                layer.msg('服务器繁忙', {icon: 5});
                            }
                        });
                        layer.close(index);
                    });
                }
            });

        });

    </script>
</block>